package one.stand.controller;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jmp.base.ShopCartApi;
import com.jmp.base.dto.*;
import lombok.extern.slf4j.Slf4j;
import one.stand.mapper.GoodSpecMapperExt;
import one.stand.mapper.GoodsMapperExt;
import one.stand.model.GoodSpec;
import one.stand.model.Goods;
import one.stand.model.ResultModel;
import one.stand.model.ShopCart;
import one.stand.service.GoodsPhotoService;
import one.stand.service.ShopCartService;
import one.stand.util.MoneyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@Slf4j
public class ShopCartController implements ShopCartApi {

    @Autowired
    private GoodsPhotoService goodsPhotoService;

    @Autowired
    private ShopCartService shopCartService;
    @Autowired
    private GoodSpecMapperExt goodSpecMapperExt;
    @Autowired
    private GoodsMapperExt goodsMapperExt;

    @Override
    public ResultModel<ShopCartListVo> list(@RequestBody ShopCartListDto request) {

        List<ShopCart> shopCarts = shopCartService.list(request.getUserId());
        Map<Integer, List<ShopCartListVo.Goods>> map = Maps.newLinkedHashMap();
        for (ShopCart shopCart : shopCarts) {
            GoodSpec goodSpec = goodSpecMapperExt.selectByPrimaryKey(shopCart.getSpecId());
            Goods goods = goodsMapperExt.selectByPrimaryKey(goodSpec.getGoodsId());
            ShopCartListVo.Goods goodsVo = ShopCartListVo.Goods.builder()
                    .shopCartId(shopCart.getShopCartId())
                    .specId(shopCart.getSpecId())
                    .num(shopCart.getNum())
                    .cardId(shopCart.getCardId())

                    .goodsId(goodSpec.getGoodsId())
                    .goodsName(goods.getGoodsName())
                    .photo(goodsPhotoService.getPath(goodSpec.getGoodsId()))
                    .specName(goodSpec.getSpecName())
                    .specAttribute(goodSpec.getSpecAttribute())
                    .price(MoneyUtil.getMoney(goodSpec.getPrice()))
                    .specNum(goodSpec.getSpecNum())
                    .build();
            if (map.containsKey(shopCart.getCompanyId())) {
                map.get(shopCart.getCompanyId()).add(goodsVo);
            } else {
                map.put(shopCart.getCompanyId(), Lists.newArrayList(goodsVo));
            }
        }
        List<ShopCartListVo.Supplier> suppliers = Lists.newArrayList();
        for (Map.Entry<Integer, List<ShopCartListVo.Goods>> entry : map.entrySet()) {
            suppliers.add(ShopCartListVo.Supplier.builder()
                    .companyId(entry.getKey())
                    .goodsList(entry.getValue())
                    .build());
        }

        ShopCartListVo vo = ShopCartListVo.builder().supplierList(suppliers).build();
        return ResultModel.success(vo);
    }

    @Override
    public ResultModel add(@RequestBody ShopCartAddDto request) {

        shopCartService.save(request.getUserId(), request.getSpecId(), request.getNum(), request.getCardId(), request.getForwardId());

        return ResultModel.success();
    }

    @Override
    public ResultModel remove(@RequestBody ShopCartRemoveDto request) {

        for (Integer id : request.getShopCartId()) {
            shopCartService.remove(id);
        }

        return ResultModel.success();
    }

    @Override
    public ResultModel edit(@RequestBody ShopCartEditDto request) {

        shopCartService.edit(request.getShopCartId(), request.getNum());

        return ResultModel.success();
    }

    @Override
    public ResultModel<ShopCartCountVo> count(@RequestBody ShopCartCountDto request) {

        Integer total = shopCartService.count(request.getUserId());

        ShopCartCountVo vo = ShopCartCountVo.builder()
                .total(Objects.nonNull(total) ? total : 0)
                .build();
        return ResultModel.success(vo);
    }
}
